package com.naiterui.ehp.bs.recommend.controller.medication;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.service.IMedicationRequireService;
import com.naiterui.ehp.bs.recommend.service.IPrescriptionService;
import com.naiterui.ehp.bs.recommend.swagger.Tags;
import com.naiterui.ehp.bs.recommend.vo.require.DrRequireVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 患者购药请求控制层
 *
 * @author guoyongxiang
 * @since 2.3.0
 */
@Slf4j
@Api(tags = SwaggerTag.DOCTOR + SwaggerTag.REQUIRE_APPLY)
@RestController
@RequestMapping("recommend/medication/require/")
public class MedicationRequireController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MedicationRequireController.class);

    @Autowired
    private IMedicationRequireService medicationRequireService;
    @Autowired
    private IPrescriptionService prescriptionService;

    @RequestMapping("dr/list")
    @ApiOperation(value = "医生端求药列表", notes = "", httpMethod = "POST", tags = {SwaggerTags.APP_DOCTOR_INQUIRER})
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态：1：待处理， 2：已处理， 99：全部", required = true, dataType = "int", paramType = "query")})
    @ResponseBody
    public ResponseVO<PageVO<DrRequireVO>> drList(Long doctorId, Integer status, PageParamVO pageParamVO) throws BusinessException {
        log.info("医生端求药列表：doctorId：{}，status：{}",doctorId, status);
        PageVO<DrRequireVO> page = medicationRequireService.getDrRequireList(doctorId, status, pageParamVO);
        return PublicService.returnResponseVO(page);
    }

    @RequestMapping("dr/suspend")
    @ApiOperation(value = "暂停用药", notes = "医生端操作暂停用药", httpMethod = "POST", tags = {Tags.REQUIRE_APPLY + Tags.APP})
    @ApiImplicitParams({@ApiImplicitParam(name = "requireId", value = "求药id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "暂停用药备注", required = true, dataType = "string", paramType = "query")})
    @ResponseBody
    public ResponseVO<Void> suspend(Long requireId, String remark) throws BusinessException {
        log.info("医生端暂停用药：requireId：{}，remark：{}", requireId, remark);
        medicationRequireService.requireSuspend(requireId, remark);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


    @RequestMapping("patient/again")
    @ApiOperation(value = "患者端申请再次购买", notes = "患者端失效和已购买处方再次求药接口", httpMethod = "POST", tags = {Tags.REQUIRE_APPLY + Tags.WX})
    @ApiImplicitParams({ @ApiImplicitParam(name = "recommendId", value = "原推荐单ID", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<Void> againRequire(Long patientId, Long recommendId) throws BusinessException {
        log.info("患者端申请再次购买：patientId：{}，recommendId：{}", patientId, recommendId);
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        if (patientId == null || recommendId == null) {
            LOGGER.error("againRequire ERROR ; illegal param : patientId={},recommendId={}", patientId, recommendId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        try {
            medicationRequireService.addAgainRequire(patientId, recommendId);
            return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
        } catch (BusinessException e) {
            throw new BusinessException(e.getCode());
        }
    }



}
